<template>
	<view>
		<view style="text-align: center; padding: 10rpx;" >
			<u-divider text="历史咨询" :textSize="16"></u-divider>
		</view>
		<view v-for="item in items" :key="item.id" style="padding: 10rpx">
			<view style="background: #fff;padding: 20rpx;font-size: 28rpx;">
				<view>{{formatDate(item.createdAt)}}</view>
				<view style=" padding: 10rpx 0;white-space: pre-wrap;">
					<u-read-more toggle :showHeight="100" :shadowStyle="shadowStyle"
						close-text="展开">{{item.content}}</u-read-more>
					<u-gap :height="5"></u-gap>
					<u-album :urls="item.urls" keyName="src"></u-album><strong></strong>
				</view>
				<view style="background-color: #f2f2f2;padding: 20rpx 10rpx;" v-if="item.replied">
					<view class="flex-row justify-between">
						<view class="flex-row items-center" v-if="item.channel=='healthConsult'">
							<view>
								<u-avatar :src="item.replyUser.meta.avatar" size="20"></u-avatar>
							</view>

							<text>{{item.replyUser.nickname}} </text>

						</view>
						<view>
							{{formatDate(item.repliedAt)}}
						</view>
					</view>


					<view style="padding: 10rpx 0;">
						{{item.reply}}
					</view>
				</view>
			</view>

		</view>
		<u-loadmore :status="status" v-if="status=='loading'||items.length>0"></u-loadmore>
		<u-empty v-if="status=='nomore'&&items.length==0" text="暂无咨询内容"></u-empty>
		<u-gap :height="scrollHeight"></u-gap>
		<view style="position: fixed;bottom: 0; width: 100vw; background-color: #fff;padding: 20rpx;" id="applyBottom">
			<view v-if="goods.id" class="flex-row items-center">
				<image :src="goods.meta.imageUrl" style="width: 30px;height: 30px; border-radius: 15px;"
					mode="aspectFill"></image>{{goods.name}}
			</view>

			<view class="flex-col justify-center ">
				<view style="text-align: center; padding: 20px;">
					点击阅读<text @click="goHelper" style="color: #0f8bda;">《健康咨询使用指南》</text>
				</view>
				<button :disabled="submited" type="primary" @click="save" size="mini"
					style="margin: 0 20rpx; font-size: 32rpx;">发起新咨询</button>
				<u-gap size="10"></u-gap>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				status: "loading",
				loaded: false,
				totalPage: 0,
				pageSize: 10,
				curpage: 0,
				items: [],
				content: "",
				searchForm: {
					start: "",
					end: "",
					pageNo: 0,
					pageSize: 15,
					channel: "",
					subChannel: "",
				},
				shadowStyle: {
					backgroundImage: "none",
					paddingTop: "0",
					marginTop: "20rpx"
				},
				urls1: [],
				goods: {},
				submited: false,
				fileList6: [],
				fileName: "",
				scrollHeight: 220,
			}
		},
		onLoad(options) {
			let curDate = new Date();
			let beginDate = new Date(curDate.setMonth(-6))
			this.$set(this.searchForm, 'start', this.$Utils.formatDate(beginDate, 'yyyy-MM-ddT00:00:00'));
			this.$set(this.searchForm, 'end', this.$Utils.formatDate(new Date(), 'yyyy-MM-ddT23:59:59'))
			if (options.channel) {
				this.$set(this.searchForm, 'channel', options.channel)
			}
			if (options.subChannel) {
				this.$set(this.searchForm, 'subChannel', options.subChannel)
				this.getGoodsDetail(options.subChannel)
			}
			this.$nextTick(() => {
				let _me = this;
				uni.getSystemInfo({
					success(res) {
						var tempHeight = res.windowHeight;
						console.log(tempHeight)
						uni.createSelectorQuery().select("#applyBottom").fields({
							size: true,
							scrollOffset: true
						}, (data) => {
							console.log(data.height)
							tempHeight -= data.height;

							console.log("减掉顶部后的高度 " + tempHeight);
							_me.scrollHeight = tempHeight - 80;
						}).exec();

					}
				});
			})

		},
		onShow() {
			this.reSearch()
		},
		onPullDownRefresh() {
			this.reSearch()
		},
		onReachBottom() {
			console.log("sddas")
			if (this.curpage >= this.totalPage) {
				this.status = 'nomore'
				return;
			}
			this.curpage++;
			this.searchForm.pageNo = this.curpage;
			this.searchData()

		},
		methods: {
			goHelper() {
				uni.navigateTo({
					url: '/pages-publics/healthHelper/healthHelper'
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'success',
						message: '上传中'
					})
				});

			},

			formatDate(date) {
				return this.$Utils.formatDate(date, 'yyyy-MM-dd HH:mm:ss')
			},
			save() {
				uni.navigateTo({
					url: '/pages-publics/MessageEdit/MessageEdit'
				})

			},
			doPost(postData) {
				this.$post({
					url: `${this.$Utils.ApiUrl}/healthConsult/joyMessages`,
					data: postData
				}).then((res) => {
					this.content = "";
					uni.showToast({
						title: '提交成功',
						icon: 'none'
					});
					this.fileList6 = [];
					this.submited = false;
					this.reSearch()
				})
			},

			getGoodsDetail(id) {
				var _self = this;
				this.$get({
					url: `${this.$Utils.ApiUrl}/products/${id}`
				}).then(res => {
					this.goods = res;
				})
			},

			reSearch() {
				this.curpage = 0;
				this.items = [];
				this.searchForm.pageNo = 0;
				this.searchData()
			},
			searchData() {
				this.status = 'loading';
				this.$get({
					url: `${this.$Utils.ApiUrl}/joyMessages/mine`,
					data: this.searchForm
				}).then(res => {
					console.log(res);
					this.totalPage = res.totalPages;
					this.items = this.items.concat(res.content);

					for (let item of this.items) {
						let urls = [];;
						if (item.meta.imageUrl0) {
							urls.push({
								src: item.meta.imageUrl0
							})
						}
						if (item.meta.imageUrl1) {
							urls.push({
								src: item.meta.imageUrl1
							})
						}
						if (item.meta.imageUrl2) {
							urls.push({
								src: item.meta.imageUrl2
							})
						}
						if (item.meta.imageUrl3) {
							urls.push({
								src: item.meta.imageUrl3
							})
						}
						if (item.meta.imageUrl4) {
							urls.push({
								src: item.meta.imageUrl4
							})
						}
						if (item.meta.imageUrl5) {
							urls.push({
								src: item.meta.imageUrl5
							})
						}
						this.$set(item, 'urls', urls);
					}

					if (res && res.last) {
						this.status = 'nomore'
					}
					uni.hideNavigationBarLoading()
					uni.stopPullDownRefresh()
				})
			}
		}
	}
</script>

<style>

</style>